package com.zdzx.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.ApplicationFunctionMapper;
import com.zdzx.mapper.ApplicationMapper;
import com.zdzx.mapper.CourseMapper;
import com.zdzx.mapper.DicMapper;
import com.zdzx.mapper.FunctionMapper;
import com.zdzx.mapper.RoleFunctionMapper;
import com.zdzx.mapper.RoleMapper;
import com.zdzx.mapper.SystemConfigMapper;
import com.zdzx.mapper.TenantMapper;
import com.zdzx.model.dic.AuthScopeType;
import com.zdzx.model.dic.MenuType;
import com.zdzx.model.service.base.vo.Application;
import com.zdzx.model.service.base.vo.ApplicationFunction;
import com.zdzx.model.service.base.vo.Dic;
import com.zdzx.model.service.base.vo.Function;
import com.zdzx.model.service.base.vo.Role;
import com.zdzx.model.service.base.vo.RoleFunction;
import com.zdzx.model.service.base.vo.SystemConfig;
import com.zdzx.model.service.base.vo.Tenant;
import com.zdzx.model.service.content.vo.Course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "下拉列表集合")
@RestController
@RequestMapping("/admin")
public class SelectListController {
	
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private FunctionMapper functionMapper;
	@Autowired
	private ApplicationMapper applicationMapper;
	@Autowired
	private ApplicationFunctionMapper applicationFunctionMapper;
	@Autowired
	private  RoleFunctionMapper roleFunctionMapper;
	@Autowired
	private TenantMapper tenantMapper;
	@Autowired
	private DicMapper dicMapper;
	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private SystemConfigMapper systemConfigMapper;
	
	@ApiOperation(value = "获取角色", notes = "")
	@GetMapping("/select/role/all")
	public Result<List<Role>> setUserRole(){
		List<Role> list = roleMapper.selectList(null);
		//System.out.println();
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list);
	}
	
	@ApiOperation(value = "获取菜单", notes = "") 
	@GetMapping("/select/function/menus/{clientType}") 
	public Result<List<Function>> menus(@PathVariable("clientType")String clientType){
		if(StringUtils.isEmpty(clientType)) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null,null);
		}
		QueryWrapper<Function> queryWrapper=new QueryWrapper<Function>();
		queryWrapper.eq("client_type", clientType);
		queryWrapper.orderByAsc("`index`");
		List<Function> list = functionMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list); 
	}
	@ApiOperation(value = "获取菜单类型", notes = "") 
	@GetMapping("/select/function/menuType") 
	public Result<List<Map<String,String>>> menuTypes(){
		List<Map<String,String>> result=new ArrayList<>();
		MenuType[] values = MenuType.values();
		for (MenuType menuType : values) {
			Map<String,String> map=new HashMap<String, String>();
			map.put("id",menuType.getKey());
			map.put("name", menuType.getValue());
			result.add(map);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), ResultCode.RESULT_SUCCESS.getMes(),result ) ;
	}
	
	@ApiOperation(value = "获取全部系统", notes = "") 
	@GetMapping("/select/application/all") 
	public Result<List<Application>> all(){
		List<Application> list = applicationMapper.selectList(null);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list);
	}
	@ApiOperation(value = "获取应用授权范围类型", notes = "") 
	@GetMapping("/select/application_scope") 
	public Result<AuthScopeType[]> getApplicationScope(){
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, AuthScopeType.values());
	}
	
	
	@ApiOperation(value = "获取应用下的功能", notes = "") 
	@GetMapping("/select/function/application/{applicationId}") 
	public Result<List<ApplicationFunction>> getAppFunction(@PathVariable("applicationId")String applicationId){
		QueryWrapper<ApplicationFunction> queryWrapper=new QueryWrapper<ApplicationFunction>();
		queryWrapper.eq("application_id", applicationId);
		List<ApplicationFunction> list = applicationFunctionMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	
	@ApiOperation(value = "获取角色下的功能", notes = "") 
	@GetMapping("/select/function/role/{roleId}") 
	public Result<List<RoleFunction>> getRoleFunction(@PathVariable("roleId")String roleId){
		QueryWrapper<RoleFunction> queryWrapper=new QueryWrapper<RoleFunction>();
		queryWrapper.eq("role_id", roleId);
		List<RoleFunction> list = roleFunctionMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	@ApiOperation(value = "获取全部租户", notes = "") 
	@GetMapping("/select/Tenant/all") 
	public Result<List<Tenant>> getTenant(){
		List<Tenant> list = tenantMapper.selectList(null);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	
	
	@ApiOperation(value = "根据类型获取字典", notes = "")
	@GetMapping("/select/dic/{type}")
	public Result<List<Dic>> getDic(@PathVariable("type")String type,@RequestParam(value = "tenantId", required = false)String tenantId,
			@RequestParam(value = "value", required = false)String value){
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("`type`", type);
		if(!StringUtils.isEmpty(tenantId)) {
			queryWrapper.eq("tenant_id", tenantId);
		}
		if(!StringUtils.isEmpty(value)) {
			queryWrapper.like("`value`",value);
		}
		List<Dic> list = dicMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list);
	}
	
	@ApiOperation(value = "根据类型获取字典", notes = "")
	@GetMapping("/select/dic/all/{type}")
	public Result<List<Dic>> getAllDic(@PathVariable("type")String type,@RequestParam(value = "tenantId", required = false)String tenantId){
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("`parent_type`", type);
		if(!StringUtils.isEmpty(tenantId)) {
			queryWrapper.eq("tenant_id", tenantId);
		}
		List<Dic> list = dicMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list);
	}
	
	@ApiOperation(value = "根据类型获取字典", notes = "")
	@GetMapping("/select/dic/list/{type}")
	public Result<List<Dic>> getDicList(@PathVariable("type")String type,@RequestParam(value = "tenantId", required = false)String tenantId){
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("parent_type", type);
		if(!StringUtils.isEmpty(tenantId)) {
			queryWrapper.eq("tenant_id", tenantId);
		}
		List<Dic> list = dicMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list);
	}
	
	
	@ApiOperation(value = "搜索课程", notes = "")
	@GetMapping("/select/scourse")
	public Result<List<Course>> getCourse(@RequestParam(value = "title", required = false)String title){
		QueryWrapper<Course> queryWrapper=new QueryWrapper<Course>();
		queryWrapper.eq("title", title);
		List<Course> list = courseMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list);
	}
	
	
	@ApiOperation(value = "获取系统配置", notes = "")
	@GetMapping("/select/systemConfig/{key}")
	public Result<String> getSystemConfig(@PathVariable("key")String key){
		QueryWrapper<SystemConfig> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("`key`", key);
		List<SystemConfig> list =  systemConfigMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, list.get(0).getValue());
	}
}

